package com.achievement.service.impl;


import com.achievement.domain.bo.HonorApplyInfoBO;
import com.achievement.domain.dto.honor.HonorApplyInfoDTO;
import com.achievement.domain.entity.HonorApplyInfoEntity;
import com.achievement.domain.param.honor.HonorApplyInfoParam;
import com.achievement.domain.vo.honor.HonorApplyInfoPersonnelVO;
import com.achievement.domain.vo.honor.HonorApplyInfoVO;
import com.achievement.mapper.HonorApplyInfoMapper;
import com.achievement.mappings.HonorApplyInfoMapping;
import com.achievement.service.IAchievementFileService;
import com.achievement.service.IHonorApplyInfoService;
import com.achievement.service.IReviewPlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.msg.service.SendMessageService;
import com.common.utils.CollectionUtils;
import com.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HonorApplyInfoServiceImpl extends ServiceImpl<HonorApplyInfoMapper, HonorApplyInfoEntity> implements IHonorApplyInfoService {

    @Autowired
    private HonorApplyInfoMapping mapping;
    @Autowired
    private HonorApplyInfoMapper mapper;
    @Autowired
    private IReviewPlanService reviewPlanService;

    @Autowired
    private IAchievementFileService fileService;

    @Autowired
    private SendMessageService sendMessageService;

    @Override
    public List<HonorApplyInfoVO> selectByPage(HonorApplyInfoParam param) {

        param.setBeginDateStart(DateUtils.dateStrHandle(param.getBeginDateStart(), param.getBeginDateFormat(), "start"));
        param.setBeginDateEnd(DateUtils.dateStrHandle(param.getBeginDateEnd(), param.getBeginDateFormat(), "end"));

        param.setEndDateStart(DateUtils.dateStrHandle(param.getEndDateStart(), param.getEndDateFormat(), "start"));
        param.setEndDateEnd(DateUtils.dateStrHandle(param.getEndDateEnd(), param.getEndDateFormat(), "end"));


        LambdaQueryWrapper<HonorApplyInfoEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StringUtils.isNotEmpty(param.getId()), HonorApplyInfoEntity::getId, param.getId());
        queryWrapper.like(StringUtils.isNotEmpty(param.getName()), HonorApplyInfoEntity::getName, param.getName());
        queryWrapper.like(StringUtils.isNotEmpty(param.getHonorName()), HonorApplyInfoEntity::getHonorName, param.getHonorName());
        queryWrapper.ge(ObjectUtils.isNotEmpty(param.getBeginDateStart()),HonorApplyInfoEntity::getBeginDate,param.getBeginDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(param.getBeginDateEnd()),HonorApplyInfoEntity::getBeginDate,param.getBeginDateEnd());
        queryWrapper.ge(ObjectUtils.isNotEmpty(param.getEndDateStart()),HonorApplyInfoEntity::getEndDate,param.getEndDateStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(param.getEndDateEnd()),HonorApplyInfoEntity::getEndDate,param.getEndDateEnd());
        if (StringUtils.isNotEmpty(param.getApplyState())&&!param.getApplyState().equals("[]")) {
            queryWrapper.in(StringUtils.isNotEmpty(param.getApplyState()), HonorApplyInfoEntity::getApplyState, Arrays.asList(param.getApplyState().split(",")));
        }
        //分组设置
        queryWrapper.eq(StringUtils.isNotEmpty(param.getApplyStateGroup()), HonorApplyInfoEntity::getApplyState, param.getApplyStateGroup());
        if (StringUtils.isNotEmpty(param.getHonorLevel())&&!param.getHonorLevel().equals("[]")) {
            queryWrapper.in(StringUtils.isNotEmpty(param.getHonorLevel()), HonorApplyInfoEntity::getHonorLevel, Arrays.asList(param.getHonorLevel().split(",")));
        }
        queryWrapper.eq(StringUtils.isNotEmpty(param.getIsAutoEnd()), HonorApplyInfoEntity::getIsAutoEnd, param.getIsAutoEnd());
        queryWrapper.eq(StringUtils.isNotEmpty(param.getIsReview()), HonorApplyInfoEntity::getIsReview, param.getIsReview());
        List<HonorApplyInfoVO> voList = this.baseMapper.selectByPage(queryWrapper);
        if (!CollectionUtils.isAnyEmpty(voList) && voList.size() > 0) {
            List<String> applyInfoIds = reviewPlanService.getApplyInfoIds();
            for (HonorApplyInfoVO vo :
                    voList) {
                if (vo.getIsReview().equals("1")) {
                    if (applyInfoIds.stream().filter(t -> t.equals(vo.getId())).collect(Collectors.toList()).size() > 0) {
                        vo.setIsCompleteChangeReview("1");
                    } else {
                        vo.setIsCompleteChangeReview("0");
                    }
                } else {
                    vo.setIsCompleteChangeReview("-1");
                }
            }
        }
        return voList;
    }

    @Override
    public List<HashMap<String, Integer>> listStatistics(HonorApplyInfoParam param){

        param.setBeginDateStart(DateUtils.dateStrHandle(param.getBeginDateStart(), param.getBeginDateFormat(), "start"));
        param.setBeginDateEnd(DateUtils.dateStrHandle(param.getBeginDateEnd(), param.getBeginDateFormat(), "end"));

        param.setEndDateStart(DateUtils.dateStrHandle(param.getEndDateStart(), param.getEndDateFormat(), "start"));
        param.setEndDateEnd(DateUtils.dateStrHandle(param.getEndDateEnd(), param.getEndDateFormat(), "end"));

        HonorApplyInfoBO bo=mapping.param2Bo(param);
        if(StringUtils.isNotEmpty(param.getApplyState())&&!param.getApplyState().equals("[]")){
            bo.setApplyStates(Arrays.asList(param.getApplyState().split(",")));
        }
        if(StringUtils.isNotEmpty(param.getHonorLevel())&&!param.getHonorLevel().equals("[]")){
            bo.setHonorLevels(Arrays.asList(param.getHonorLevel().split(",")));
        }
        return mapper.listStatistics(bo);
    }
    @Override
    public List<HonorApplyInfoPersonnelVO> selectHonorApplyInfoPersonnelByPage(String userId){
        List<HonorApplyInfoPersonnelVO> list = mapper.selectHonorApplyInfoPersonnelByPage(userId);
        return list;
    }

    @Override
    public List<HonorApplyInfoEntity> getListByConditions(LambdaQueryWrapper<HonorApplyInfoEntity> queryWrapper) {
        List<HonorApplyInfoEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public HonorApplyInfoVO getInfoById(String id) {
        HonorApplyInfoEntity entity = this.getById(id);
        HonorApplyInfoVO vo = mapping.entity2Vo(entity);
        String fileIds = entity.getFileId();
        if (com.common.utils.StringUtils.hasText(fileIds)) {
            String[] fileIdArr = fileIds.split(",");
            List<Map<String, String>> fileInfo = fileService.getFileInfoByFileIds(fileIdArr);
            vo.setFileList(fileInfo);
        }
        return vo;
    }

    @Override
    public boolean add(HonorApplyInfoDTO dto) {
        HonorApplyInfoEntity entity = mapping.dto2Entity(dto);
        return this.save(entity);
    }


    @Override
    public boolean update(HonorApplyInfoDTO dto) {
        HonorApplyInfoEntity entity = mapping.dto2Entity(dto);
        return this.updateById(entity);
    }
    @Override
    public boolean updateStatusById(HonorApplyInfoDTO dto){
        HonorApplyInfoEntity entity = mapping.dto2Entity(dto);
        // 点击启动
        if ("1".equals(dto.getApplyState())){
            HonorApplyInfoEntity entityOld=this.getById(dto.getId());
            String title = entityOld.getName();
            // 使用 SimpleDateFormat 进行转换
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String endDate = sdf.format(entityOld.getEndDate());
            sendMessageService.batchSendMessageDeclare(title,endDate);
        }
        return this.updateById(entity);
    }
    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }
}
